package com.example.minemodule.ui;

import static com.example.minemodule.MineConstants.LOGOUT_RESULT;
import static com.example.minemodule.MineConstants.SETTING_REQUEST_ID;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.widget.NestedScrollView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.examination.mlib.constants.ARouterConstants;
import com.examination.mlib.constants.ClickApi;
import com.examination.mlib.constants.KitArouterConstants;
import com.examination.mlib.constants.ProjectConfigure;
import com.examination.mlib.interfaceapi.MineBackApi;
import com.example.minemodule.R;
import com.example.minemodule.Utils.MineUtils;
import com.example.minemodule.databinding.MineFragmentBinding;
import com.example.minemodule.viewmodel.MineViewModel;
import com.yilijk.base.base.BaseFragment;
import com.yilijk.base.utils.ClickUtils;
import com.yilijk.base.utils.SharedPreferencesUtils;


/**
 * Created by SHICHENG
 * <p>
 * Updated bg SHICHENG
 * 2021/10/25
 * <p>
 * Updated bg SHICHENG
 * 2021/10/27
 */
@Route(path = ARouterConstants.MineFragment)
public class MineFragment extends BaseFragment<MineViewModel, MineFragmentBinding> {

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_mine;
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void processLogic(Bundle savedInstanceState) {
        mViewModel.setBinding(getActivity(), binding);
        mViewModel.setTitleBar();
        mViewModel.setHeader();
        mViewModel.initOrder();
        mViewModel.initWelfare();
        mViewModel.initInquiryDrug();
        mViewModel.initHealth();
        mViewModel.initCommon();
        binding.setEmployeeAccountUrl("https://cdn.yilijk.com/images/vbp/patientimg/neigouicon.png");
        binding.veiwWaterMaster.refreshEmail("");
    }


    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void dispostData() {
        mViewModel.requestData(this);
        ClickUtils.sendEvent(ClickApi.Mine.Mine_show, getActivity(), "我的载入");
    }


    @Override
    protected void setListener() {
        binding.nestedScrollview.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                MineBackApi mineBackApi = (MineBackApi) ARouter.getInstance().build(ARouterConstants.MineModuleImpl).navigation();
                Activity activity = getActivity();
                if (activity != null) {
                    mineBackApi.hideImLayout(activity);
                }
            }
        });
        /**
         * 设置点击
         */
        ClickUtils.setFastOnClickListener(binding.mineFragmentSetting, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClickUtils.sendEvent(ClickApi.Mine.click_setting, getActivity(), "设置点击");
                if (!ProjectConfigure.Open.isOpenSetting) {
                    startActivityForResult(
                            new Intent(getActivity(), SettingActivity.class),
                            SETTING_REQUEST_ID
                    );
                } else {
                    MineUtils.push(getActivity(), KitArouterConstants.MY_BUNDLE, KitArouterConstants.Mine.Setting);
                }
            }
        });
        /**
         * 头像
         */
        ClickUtils.setFastOnClickListener(binding.mineHeader.mineHeaderAvator, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClickUtils.sendEvent(ClickApi.Mine.click_avatar, getActivity(), "头像点击");
                MineUtils.push(getActivity(), KitArouterConstants.MY_BUNDLE, KitArouterConstants.Mine.AccountSetting);
            }
        });
        /**
         * 全部订单
         */
        ClickUtils.setFastOnClickListener(binding.mineOrder.orderMore, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClickUtils.sendEvent(ClickApi.Mine.click_order_more, getActivity(), "全部订单点击");
                Bundle bundle = new Bundle();
                bundle.putString("flag", "");
                MineUtils.push(getActivity(), KitArouterConstants.MY_BUNDLE, KitArouterConstants.Mine.MyOrder, bundle);
            }
        });
        /**
         * 员工内购点击
         */
        ClickUtils.setFastOnClickListener(binding.ivEmployeeAccount, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClickUtils.sendEvent(ClickApi.Mine.click_employee, getActivity(), "员工内购点击");
                // 员工内购
                Bundle bundle = new Bundle();
                bundle.putInt("id", 3);
                MineUtils.push(getActivity(), KitArouterConstants.CART_DOCTORBUNDLE, KitArouterConstants.Cart.Packs, bundle);
            }
        });
        /**
         * 悬浮球点击事件处理
         */
        ClickUtils.setFastOnClickListener(binding.suspendedBallView, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClickUtils.sendEvent(ClickApi.Mine.click_ball, getActivity(), "悬浮球点击");

                binding.suspendedBallView.show();
            }
        });
    }




    @Override
    public void onClick(View v) {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SETTING_REQUEST_ID && resultCode == LOGOUT_RESULT) {
            boolean b = SharedPreferencesUtils.getInstance().removeAll();
            if (b) {
                MineBackApi mineBackApi = (MineBackApi) ARouter.getInstance().build(ARouterConstants.MineModuleImpl).navigation();
                mineBackApi.logoutLive();
                ARouter.getInstance().build(ARouterConstants.CenterActivity).navigation();
                if (getActivity() != null && !getActivity().isFinishing() && MineUtils.logoutIM()) {
                    getActivity().finish();
                }
            }
        }
    }

}